<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Services\SearchService;
use Illuminate\Http\Request;

/**
 * 前台搜索控制器
 * 
 * 职责：
 * - 处理前台搜索相关的HTTP请求
 * - 调用 SearchService 处理业务逻辑
 * - 返回JSON响应
 */
class SearchController extends Controller
{
    /**
     * 搜索服务
     * 
     * @var SearchService
     */
    protected SearchService $searchService;

    /**
     * 构造器
     */
    public function __construct(SearchService $searchService)
    {
        $this->searchService = $searchService;
    }

    /**
     * 前台搜索（只搜索已发布的内容）
     * 
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function search(Request $request)
    {
        try {
            $query = $request->input('q', '');
            $results = $this->searchService->search($query);
            
            return response()->json($results);
        } catch (\Exception $e) {
            return response()->json(['error' => '搜索请求失败'], 500);
        }
    }

    /**
     * 后台全局搜索（搜索所有内容）
     * 
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function globalSearch(Request $request)
    {
        try {
            $query = $request->input('q', '');
            $results = $this->searchService->globalSearch($query);
            
            return response()->json($results);
        } catch (\Exception $e) {
            return response()->json(['error' => '搜索请求失败'], 500);
        }
    }
} 